<template>
  <div>
    <div
      ref="myChart"
      class="myChart"
      :style="{ width: '100%', height: '300px' }"
    ></div>
  </div>
</template>

<script>
import EleResize from "@/assets/Tools/esresize.js";
let echarts = require("echarts");
export default {
  props: {
    title: {
      type: String,
      required: true,
    },
    // 数字有默认值
    dataX: {
      type: Array,
      required: true,
    },
    dataY: {
      type: Array,
      required: true,
    },
  },
  mounted() {
    this.drawLine();
  },
  methods: {
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(this.$refs.myChart);

      // 绘制图表
      myChart.setOption({
        title: { text: this.title },
        tooltip: {},
        xAxis: {
          data: this.dataX,
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: this.dataY,
          },
        ],
      });

      //自适应大小

      var resizeDiv = this.$refs.myChart;
      EleResize.on(resizeDiv, () => {
        myChart.resize();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.myChart {
  margin-top: 0px;
}
</style>
